<template>
  <div class="specialOffer">
    <TabBar
      ref="tabBar"
      :tabActive="tabActive"
      @changeSpecial="changeSpecial"
    />
    <TopBar
      ref="topBar"
      @search="search"
      @changeSortTab="changeSortTab"
      :showBanner="showBanner"
      @changeTab="changeTab"
      :dataSourse="banner"
    />
    <!-- //商品列表 -->
    <LadderGoodsList
      :is119="is119"
      :isPreview="isPreview"
      @showCards="showCards"
      :dataSoures="ladderGoodsList"
      :finished="finished"
      :loading="loading"
      @load="findPageTieredPricingGoods()"
    />
    <!-- //弹窗详情 -->
    <DialogInfo
      :is119="is119"
      :everyDayInfo="everyDayInfo"
      :show="show"
      @close="close"
      type="阶梯价"
    />
    <div class="upgrade" @click="goTop" v-if="goTopShow">
      <img src="https://image.hezongyy.com/pcimage/119h5/goup.png" alt="" />
    </div>
    <van-overlay :show="activeLoadingShow" @click="show = false">
      <div class="wrapper" @click.stop>
        <div class="activeLoading"><img :src="activeLoading" alt="" /></div>
      </div>
    </van-overlay>
  </div>
</template>
<script>
import { getRequest } from "@/api/indexGet";
import { postRequest } from "@/api/index";
import url from "@/axios/apiUrl";
import Xpng from "@/assets/image/X.png";
import TopBar from "./topBar.vue";
import TabBar from "./tabBar.vue";
import DialogInfo from "@/components/DialogInfo";
import activeLoading from "@/assets/image/activeLoading.gif";
import LadderGoodsList from "@/components/LadderGoodsList";
export default {
  components: {
    TabBar,
    TopBar,
    DialogInfo,
    LadderGoodsList,
  },
  data() {
    return {
      // showColor: false,
      goTopShow: false,
      showBanner: true,
      showGoodsList: false,
      activeLoading,
      activeLoadingShow: false,
      Xpng, //图片关闭按钮
      loading: false, //加载
      finished: false, //加载完成
      from: {
        pageNumber: 1,
        pageSize: 40,
        keyWord: "", //搜索关键字
        isChoose: 0, //搜索类型 0 查询全部  1 已加入购物车  2未选择购物车
        onSaleType: this.$route.query.key - 0,
        isWeb: 0,
        category: "1", //判断是否去专区促销活动还是平台促销活动
        drugType: 0, //药品的分类的类别 （ 0不限，1 成药(西药中成药)，2中药饮片(中药)，3保健食品，4器械  5 日化品）
        // testFlag: true,
      },
      //可配置参数数据
      page: {
        saleType: this.$route.query.key - 0,
        category: 1,
        isWeb: 0,
      },
      show: false, //弹窗开关
      everyDayInfo: {}, ///弹窗信息详情
      banner: [], // banner
      ladderGoodsList: [], //阶梯价列表
      // num: '', //阶梯价已加购数量信息
      is119: false,
      isPreview: false,
      tabActive: "1", // 1 /活动专区  2 /平台活动
    };
  },
  async created(){
    await this.getTopBar();
    this.findPageTieredPricingGoods()
  },
  async mounted() {
    // this.findPageNum()
    // this.getBanner();
    let node = document.querySelector(".ladder_goods_area");
    node.onscroll = () => {
      if (
        node.scrollTop > 150 &&
        this.ladderGoodsList &&
        this.ladderGoodsList.length > 1
      ) {
        this.showBanner = false;
      } else if (node.scrollTop == 0) {
        this.showBanner = true;
        this.goTopShow = false;
      }
      if (node.scrollTop > 600 && this.ladderGoodsList) {
        this.goTopShow = true;
      }
    };
  },
  beforeDestroy() {
    clearInterval(this.timeName);
    clearInterval(this.curTime);
  },
  methods: {
    //获取可配置参数
    async getTopBar() {
      const res = await postRequest(url.getPromotionPageInfo, this.page);
      if (!res.data.content) return;
      if(res.data.content.promotionLabelVOList.length != 0){
        this.from.drugType = res.data.content.promotionLabelVOList[0].parameter;
      }else{
        this.from.drugType = 0
      }
      if(res.data.content.promotionTabVOList.length != 0){
        this.from.category = res.data.content.promotionTabVOList[0].parameter;
      }else{
        this.from.category = 1
      }
      this.banner = res.data.content.promotionBannerVOList;
      this.$refs.topBar.options = res.data.content.promotionLabelVOList;
      this.$refs.tabBar.tabList = res.data.content.promotionTabVOList;
    },
    //活动banner阶梯价
    // async getBanner() {
    //   let res = await postRequest(url.adPost, {
    //     pageName: "tieredSpecialPerformance",
    //     clientType: 1,
    //   });
    //   if (res.data.code == "000000") {
    //     this.banner = res.data.content.tieredSpecialPerformanceBanner.items;
    //   } else {
    //     this.$toast(res.data.message);
    //   }
    // },
    //119时间
    async time119() {
      let res = await getRequest(url.time119);
      if (res.data.code == "000000") {
        // this.is119 = res.data.content.is119
        // this.isPreview = res.data.content.isPreview
        // this.startTime = res.data.content.start
        // this.nowTime = res.data.content.now
        // this.nowTime = 1634287318784
        // this.startTime = 1634287322784
      }
      // this.countDownInit()
      // this.getNowTime()
    },
    // // 获取当前时间
    // countDownInit() {
    //   clearInterval(this.curTime)
    //   this.curTime = setInterval(() => {
    //     this.nowTime = this.nowTime + 1000
    //   }, 1000)
    // },
    // // 计算时间
    // getNowTime() {
    //   this.timeName = setInterval(() => {
    //     if (this.nowTime >= this.startTime) {
    //       this.showColor = true
    //     }
    //     if (this.showColor) {
    //       this.from.pageNumber = 1
    //       this.findPageTieredPricingGoods()
    //       clearInterval(this.timeName)
    //       clearInterval(this.curTime)
    //     }
    //   }, 1000)
    // },
    changeSortTab(item) {
      this.from.keyWord = "";
      this.from.drugType = item.parameter;
      this.from.pageNumber = 1;
      this.from.isChoose = 0;
      this.findPageTieredPricingGoods();
      // this.findPageNum()
    },
    //阶梯价获取数量
    // async findPageNum() {
    //   let from = {
    //     pageNumber: 1,
    //     pageSize: 10,
    //     keyWord: this.from.keyWord, //搜索关键字
    //     searchType: 1, //搜索类型 0 查询全部  1 已加入购物车  2未选择购物车
    //     // goodsTypeList: this.from.goodsTypeList,
    //   }
    //   let res = await postRequest(url.findPageNum, from)
    //   if (res.data.code == '000000') {
    //     this.num = res.data.content
    //   } else if (res.data.message == '系统异常') {
    //     this.$toast('网络异常，请稍后再试')
    //   } else if (res.data.code != '000000') {
    //     this.$toast(res.data.message)
    //   }
    // },
    //集采标识显示
    getLabelImg(labelId) {
      if (labelId == 21)
        return "https://image.hezongyy.com/pcimage/119h5/h5_jc.png";
      if (labelId == 8)
        return "https://image.hezongyy.com/pcimage/119h5/h5_jm.png";
    },
    //阶梯价跳转原生详情页面
    goToDetail(item) {
      window.location.href =
        this.pathURL + "nativeApp/goodsDetails?id=" + item.id;
    },
    //tab筛选
    changeTab(type) {
      this.from.pageNumber = 1;
      if (type == 0) {
        this.from.isChoose = 0;
      } else if (type == 1) {
        this.from.isChoose = 1;
      } else if (type == 2) {
        this.from.isChoose = 2;
      }
      this.findPageTieredPricingGoods();
      //切换回到顶部
      this.goTop();
    },
    //回到顶部
    goTop() {
      let node = document.querySelector(".ladder_goods_area");
      if (node) {
        node.scrollTop = 0;
      } else {
        node.scrollTop = 0;
      }
    },
    //关闭弹窗
    close() {
      this.show = false;
      // this.findPageNum()
      // this.from.pageNumber = 1
      this.findPageTieredPricingGoods();
    },
    //加购弹窗
    showCards(item) {
      this.show = true;
      this.everyDayInfo = item;
    },
    //商品列表
    async findPageTieredPricingGoods() {
      this.loading = true;
      if (this.from.pageNumber == 1) {
        this.activeLoadingShow = true;
      }
      let res = await postRequest(url.getPromotionGoodsPage, this.from);
      if (res.data.code == "000000") {
        this.activeLoadingShow = false;
        if (!res.data.content) {
          this.finished = true;
        }
        if (this.from.pageNumber == 1) {
          this.ladderGoodsList = [];
        }
        if (res.data.content.list == []) {
          this.finished = true;
          return;
        }
        this.loading = false;
        this.from.pageNumber++;
        if (res.data.content) {
          this.ladderGoodsList = this.ladderGoodsList.concat(
            res.data.content.list
          );
        }
        if (res.data.content && res.data.content.list.length == 0) {
          // 数据全部加载完成
          this.finished = true;
        } else {
          this.finished = false;
        }
        if (this.ladderGoodsList) {
          this.ladderGoodsList.map((obj) => {
            this.$set(obj, "initializeMinimum", 1);
            this.$set(obj, "maximum", obj.maxBuyNumber);
            this.goodsAdd.pageInit(obj, this);
          });
        }
      } else {
        this.finished = true;
        this.activeLoadingShow = false;
        this.ladderGoodsList = []
        this.$toast(res.data.message);
      }
    },
    //商品列表搜索
    async search(value) {
      this.from.keyWord = value;
      this.from.pageNumber = 1;
      this.findPageTieredPricingGoods();
      // this.findPageNum()
      // let node = document.querySelector('.ladder_goods_area')
      // node.scrollTop = 0
      this.goTop();
    },
    //选择tab切换活动专区 平台活动
    async changeSpecial(item) {
      // 1每日特价2常规特价
      // this.routineGoodsList = []
      this.from.pageNumber = 1
      // this.temp.keyWord = ''
      // this.everyDayKeyWord = ''
      this.tabActive = item;
      this.from.category = item;
      // this.$nextTick()
      this.findPageTieredPricingGoods();
      this.goTop();
    },
  },
  beforeDestroy() {},
};
</script>
<style lang="less" src="./index.less" scoped></style>
